package tech.autumn.autoconfigure.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import tech.autumn.exception.resolver.HandlerAllExceptionResolver;
import tech.autumn.exception.resolver.HandlerBusinessExceptionResolver;
import tech.autumn.exception.resolver.HandlerValidationExceptionResolver;

import tech.autumn.exception.ExceptionHandler;
import java.util.List;

/**
 * 异常处理器自动配置
 * @author koko
 * @date 2025年01月21日 14:50
 */
@Configuration
@ConditionalOnWebApplication
public class ExceptionHandlerAutoConfiguration {

    protected static final Logger LOG = LoggerFactory.getLogger(ExceptionHandlerAutoConfiguration.class);


    @Configuration
    public static class WebMvcConfig implements WebMvcConfigurer {

        @Autowired(required = false)
        private ExceptionHandler exceptionHandler;

        @Override
        public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
            // 异常处理链
            exceptionResolvers.add(0, new HandlerBusinessExceptionResolver());
            exceptionResolvers.add(0, new HandlerValidationExceptionResolver());
//            exceptionResolvers.add(0, new HandlerUnauthenticatedExceptionResolver());
            // 业务系统配置的Resolver将在中间
            exceptionResolvers.add(new HandlerAllExceptionResolver(exceptionHandler));
            LOG.debug("Note 配置异常处理解析器");
        }

    }


}
